package com.markus.code.二叉树.study;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/3 15:23
 * todo:
 */
public class PchDemo {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        return hasPathSumCore(root.left, sum - root.val) || hasPathSumCore(root.right, sum - root.val);
    }

    public boolean hasPathSumCore(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        if (root.left == null && root.right == null) {
            if (sum == root.val) {
                return true;
            } else {
                return false;
            }
        } else {
            return hasPathSumCore(root.left, sum - root.val) || hasPathSumCore(root.right, sum - root.val);
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        PchDemo demo = new PchDemo();
        System.out.println(demo.hasPathSum(root,1));
    }
}
